package com.example.dmgame.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.dmgame.R;
import com.example.dmgame.activity.ChapterContentActivity;
import com.example.dmgame.activity.NlfHeaderWebView;
import com.example.dmgame.adapter.NewsAdapter;
import com.example.dmgame.bean.ChapterListItem;
import com.example.dmgame.utils.JsonAsyncTask;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class News_listviewFragment extends Fragment implements
		OnItemClickListener {
	private ListView news_listView;
	private NewsAdapter newsAdapter;

	int[] typeids = { 1, 2, 151, 152, 153, 154, 196, 197, 199, 25 };

	private int row = 10;
	private int typeid;
	private int maxRow = 0;
	private LinearLayout progressLayout;

	private List<ChapterListItem> list = new ArrayList<ChapterListItem>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.news_listview, container, false);
		news_listView = (ListView) view.findViewById(R.id.chapterListView);
		Log.i("MainActivity", "---->>news_listView=" + news_listView);
		
		progressLayout=(LinearLayout)view.findViewById(R.id.progressLayoutId);

		final ImageView imageView = new ImageView(getActivity());
		imageView.setScaleType(ScaleType.FIT_XY);
		imageView.setImageResource(R.drawable.default1);
		news_listView.addHeaderView(imageView);

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case 0:
					imageView.setImageResource(R.drawable.default1);
					break;
				case 1:
					imageView.setImageResource(R.drawable.default2);
					break;
				case 2:
					imageView.setImageResource(R.drawable.default3);
					break;
				default:
					break;
				}
			}
		};

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					try {
						Thread.sleep(2000);
						int what = (int) (System.currentTimeMillis() / 1000 % 3);
						Message message = new Message();
						message.what = what;
						handler.sendMessage(message);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();

		Bundle bundle = getArguments();
		Log.i("---News_listView-->>", "---->>bundle=" + bundle);
		int index = (Integer) bundle.get("typeid");
		typeid = typeids[index];

		newsAdapter = new NewsAdapter(list, getActivity(), news_listView);
		Log.i("MainActivity", "---->>newsAdapter=" + newsAdapter);
		news_listView.setAdapter(newsAdapter);
		// newsAdapter.notifyDataSetChanged();

		loadChapterData();
		news_listView.setOnItemClickListener(this);
		news_listView.setOnScrollListener(new OnScrollListener() {
			private boolean isBottom = false;
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					break;
				case OnScrollListener.SCROLL_STATE_FLING:
					break;
				case OnScrollListener.SCROLL_STATE_IDLE:
					if(isBottom){
						Log.i("News_listviewFragment", "---->>row:"+row+",maxRow:"+maxRow);
						if(row<=maxRow){
							loadChapterData();
						}
					}
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				isBottom = (firstVisibleItem+visibleItemCount)==totalItemCount;
			}
		});
		return view;
	}

	private void loadChapterData() {
		JsonAsyncTask task = new JsonAsyncTask(new JsonAsyncTask.Callback() {

			@Override
			public void sendData(List<ChapterListItem> chapterListItems, int row) {
				// TODO Auto-generated method stub
				Log.i("NewsFragment", "--->>list=" + list
						+ ",chapterListItems=" + chapterListItems);
				maxRow=row;
				list.clear();
				list.addAll(chapterListItems);
				newsAdapter.notifyDataSetChanged();
				progressLayout.setVisibility(View.GONE);
			}
		}, row);
		task.execute(loadChapterURL(row+=5, typeid));
		progressLayout.setVisibility(View.VISIBLE);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if (position == 0) {
			Intent toWeb = new Intent(getActivity(), NlfHeaderWebView.class);
			startActivity(toWeb);
		} else {
			Intent intent = new Intent(getActivity(),
					ChapterContentActivity.class);
			intent.putExtra("id", list.get(position-1).getId());
			intent.putExtra("typeid", typeid);
			startActivity(intent);
		}
	}

	private String loadChapterURL(int row, int typeid) {
		return "http://www.3dmgame.com/sitemap/api.php?row=" + row + "&typeid="
				+ typeid + "&paging=1&page=n";
	}
}
